package Pages;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import Others.ButtonTabComponent;
import TableAdditions.BillAdditionMenu;
import Tables.DailySalesTableInterface;
import Tables.StorageVariationTableInterface;


public class DailyProcessesPage extends JFrame implements ActionListener {
	public static int tabNum=0;
	JTabbedPane jtp;
	JPanel jp1;
	
	JLabel ResimTutucu1 = new JLabel();// MBS catch the image
	ImageIcon Resim1 = new ImageIcon(getClass().getResource("DailyProcess.png"));
	
	JPanel[] panel;
	
	JButton dailySalesButton = new JButton("Günlük Satışlar");
    JButton dailyReceivingButton = new JButton("Günlük Mal Alış");
    JButton dailyStorageVariationButton = new JButton("Günlük Depo Hareketleri");
    JButton BillButton = new JButton("Fatura Ekranı");
    
    ///////////////////////////  for Time
    public JLabel show_time = new JLabel("Show Time");
    public JLabel Show_date = new JLabel("Show Date");

    DateFormat dateFormat2 = new SimpleDateFormat("h:mm:ss a");
    java.util.Date date2;
    long msec;
	//////////////////////////////
    
    StorageVariationTableInterface storageVariationTable;
    DailySalesTableInterface dailySalesTable;
    BillAdditionMenu bill;
    
    public DailyProcessesPage() {
    	super("Günlük İşlemler");
    	tabNum=0;
	
    	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jtp = new JTabbedPane(JTabbedPane.TOP);
        //This creates the template on the windowed application that we will be using
        getContentPane().add(jtp);
        jp1 = new JPanel();		//This will create the first tab
        

        JPanel panel1 = new JPanel();
        JPanel panel[]=new JPanel[60];
        for(int i=0;i<60;i++)
    		panel[i]=new JPanel();
        panel1.setLayout(new GridBagLayout());
        
        ResimTutucu1.setIcon(Resim1);
        addItem(panel1, ResimTutucu1, 0, 0, 5, 2, GridBagConstraints.NORTH);
        
        Font f= new Font("SansSerif", Font.PLAIN, 13);
        DateFormat dateFormat = new SimpleDateFormat("EEEEEEEEE: dd MMMMMMMMM, yyyy.");
        java.util.Date date = new java.util.Date();
        String dateTime = dateFormat.format(date);
        Show_date = new JLabel(dateTime);
        Show_date.setFont(f); 
        show_time= new JLabel("");
        updateDateTime();
        show_time.setFont(f);
        
        Box showDateTimeBox = Box.createVerticalBox();
        JLabel showDateTimeGroup = new JLabel();
        showDateTimeGroup.add(Show_date);
        showDateTimeGroup.add(show_time);
        showDateTimeBox.add(Show_date);
        showDateTimeBox.add(show_time);
        showDateTimeBox.setBorder(BorderFactory.createTitledBorder(""));
        addItem(panel1, showDateTimeBox, 0, 3, 1, 1, GridBagConstraints.EAST);
        
        dailySalesButton.setPreferredSize(new Dimension(200,40));
        dailyReceivingButton.setPreferredSize(new Dimension(200,40));
        dailyStorageVariationButton.setPreferredSize(new Dimension(220,40));
        BillButton.setPreferredSize(new Dimension(200,40));
        
        dailySalesButton.addActionListener(this);
        dailyReceivingButton.addActionListener(this);
        dailyStorageVariationButton.addActionListener(this);
        BillButton.addActionListener(this);
        
        addItem(panel1,dailySalesButton,0,4,1,1,GridBagConstraints.EAST);
		addItem(panel1,dailyReceivingButton,1,4,1,1,GridBagConstraints.EAST);
		addItem(panel1,dailyStorageVariationButton,2,4,1,1,GridBagConstraints.EAST);
		addItem(panel1,BillButton,0,5,1,1,GridBagConstraints.SOUTHEAST);
		
		for(int i=0;i<12;i++)
		{
			if(i<1){
			addItem(panel1,panel[i+10],4,i+4,1,1,GridBagConstraints.CENTER);
			addItem(panel1,panel[i+20],5,i+4,1,1,GridBagConstraints.CENTER);
			}else{
				addItem(panel1,panel[i],0,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+12],1,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+24],2,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+36],3,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+48],4,i+4,1,1,GridBagConstraints.CENTER);
			}
		}
        
	       jp1.add(panel1);

	       jtp.addTab("Günlük İşlemler", jp1); 

        this.setVisible(true);
        
	}
    
    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(5, 5, 5, 5);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
      }
	private void updateDateTime()
    {
    Thread th = new Thread(new Runnable()
        {
  
		public void run()
            {
            while(true)
                {
                date2 = new java.util.Date();
                msec = date2.getTime();
                //msec += (7.5)*60*60*1000L;
                date2.setTime(msec);
                String dateTime = dateFormat2.format(date2.getTime());  
                show_time.setText(dateTime);
                show_time.updateUI();
                }
            }
        });
    th.start();
    }


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==dailySalesButton){
			++tabNum;
			dailySalesTable=new DailySalesTableInterface();
			javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                		jtp.addTab("Günlük Satışlar", dailySalesTable);
                		jtp.setSelectedComponent(dailySalesTable);
                		initTabComponent(tabNum);
                }
            });
            
		}
		else if(e.getSource()==dailyReceivingButton){
	
			
		}else if(e.getSource()==dailyStorageVariationButton){
			++tabNum;
			storageVariationTable=new StorageVariationTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Depo Hareketleri", storageVariationTable);
                	jtp.setSelectedComponent(storageVariationTable);
                	initTabComponent(tabNum);
                	
                }
            });
		}else if(e.getSource()==BillButton){
			//++tabNum;
			//bill=new BillAdditionMenu();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	try {
    					BillAdditionMenu frame = new BillAdditionMenu();
    					frame.repaint();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
                	
                }
            });
		}
		
		
		
	}
	   public void runTest() {
	    	
	        //tabComponentsItem.setSelected(true);
	        jtp.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
	        //scrollLayoutItem.setSelected(false);
	        setSize(new Dimension(1000, 750));
	        setLocationRelativeTo(null);
	        setVisible(true);
	    }
	   private void initTabComponent(int i) {
  		jtp.setTabComponentAt(i,new ButtonTabComponent(jtp,3));
  }  


}
